/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.slideme.atom.parser.entity.AtomEntityContext;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

public class Entry {

    private String id;

    private Title title;

    private String updated;

    private Collection<Author> authors;

    private Content content;

    private Collection<Link> links;

    private String published;

    private Summary summary;

    private Category category;

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Summary getSummary() {
        return summary;
    }

    public void setSummary(Summary summary) {
        this.summary = summary;
    }

    public String getPublished() {
        return published;
    }

    public void setPublished(String published) {
        this.published = published;
    }

    public void addLink(Link link) {
        links.add(link);
    }

    public Collection<Link> getLinks() {
        return links;
    }

    public void setLinks(Collection<Link> links) {
        this.links = links;
    }

    public Entry() {
        authors = new ArrayList<Author>();
        links = new ArrayList<Link>();
    }

    public void addAuthor(Author author) {
        authors.add(author);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Title getTitle() {
        return title;
    }

    public void setTitle(Title title) {
        this.title = title;
    }

    public String getUpdated() {
        return updated;
    }

    public void setUpdated(String updated) {
        this.updated = updated;
    }

    public Collection<Author> getAuthors() {
        return authors;
    }

    public void setAuthors(Collection<Author> authors) {
        this.authors = authors;
    }

    public Content getContent() {
        return content;
    }

    public void setContent(Content content) {
        this.content = content;
    }

    public static Entry marshalFrom(KXmlParser parser, AtomEntityContext ctx) throws IOException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }

        Entry entry = new Entry();
        try {
            //parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "entry");
            while (parser.nextTag() == XmlPullParser.START_TAG) {
                String tagName = parser.getName();
                if (tagName.equals("id")) {
                    entry.setId(parser.nextText());
                } else if (tagName.equals("title")) {
                    entry.setTitle(Title.marshalFrom(parser, ctx));
                } else if (tagName.equals("author")) {
                    entry.addAuthor(Author.marshalFrom(parser));
                } else if (tagName.equals("link")) {
                    entry.addLink(Link.marshalFrom(parser, ctx));
                } else if (tagName.equals("updated")) {
                    entry.setUpdated(parser.nextText());
                } else if (tagName.equals("published")) {
                    entry.setPublished(parser.nextText());
                } else if (tagName.equals("content")) {
                    entry.setContent(Content.marshalFrom(parser, ctx));
                } else if (tagName.equals("summary")) {
                    entry.setSummary(Summary.marshalFrom(parser, ctx));
                } else if (tagName.equals("category")) {
                    entry.setCategory(Category.marshalFrom(parser, ctx));
                }
            }
        } catch (XmlPullParserException e) {
            throw new IOException("SLIDE-000-010:" + e.toString());
        }
        return entry;
    }
}
